package supercafe.core;

import java.util.Vector;
import supercafe.core.R;
import supercafe.defines.IntentAction;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Filterable;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class OrderItemAdapter extends BaseAdapter implements Filterable, OnClickListener, android.content.DialogInterface.OnClickListener {
    
	private LayoutInflater mInflater;
    private Context context;
	private Vector<OrderItem> listItems;
	private int position = 0;
	
	private int layoutId;
	private AlertDialog.Builder builder = null;
	private AlertDialog alert = null;

	private Intent noticeIntent = null;
	    
    public OrderItemAdapter(Context context, int layoutId, Vector<OrderItem> list) {
	      // Cache the LayoutInflate to avoid asking for a new one each time.
	      mInflater = LayoutInflater.from(context);
	      this.context = context;
	      this.layoutId = layoutId;
	      listItems = list;
	}

    public void setNoteToCurrentItemLine(String value) {
		listItems.get(this.position).setNote(value);
	}

	public Vector<OrderItem> getListItems() {
		return this.listItems;
	}


    public View getView(final int position, View convertView, ViewGroup parent) {
      // A ViewHolder keeps references to children views to avoid
      // unnecessary calls
      // to findViewById() on each row.
    	//Toast.makeText(this.context, "getView - position = "+position, 3000).show();
      ViewHolder holder;
      
      // When convertView is not null, we can reuse it directly, there is
      // no need
      // to re-inflate it. We only inflate a new View when the convertView
      // supplied
      // by ListView is null.
      if (convertView == null) {
        convertView = mInflater.inflate(layoutId, null);

        // Creates a ViewHolder and store references to the two children
        // views
        // we want to bind data to.
        holder = new ViewHolder();
        holder.itemName = (TextView) convertView.findViewById(R.id.itemName);
        holder.btnMinus = (ImageButton) convertView.findViewById(R.id.btnMinus);
        holder.itemQuanlity = (TextView) convertView.findViewById(R.id.itemQuanlity);
        holder.btnPlus = (ImageButton) convertView.findViewById(R.id.btnPlus);
        holder.btnNote = (ImageButton) convertView.findViewById(R.id.btnNote);
        holder.btnDelete = (ImageButton) convertView.findViewById(R.id.btnDelete);
        
        //convertView.setOnClickListener(this);
        holder.itemName.setOnClickListener(this);
        holder.itemQuanlity.setOnClickListener(this);
        holder.btnDelete.setOnClickListener(this);
        holder.btnMinus.setOnClickListener(this);
        holder.btnPlus.setOnClickListener(this);
        holder.btnNote.setOnClickListener(this);
        
        
        convertView.setTag(holder);
      } else {
        // Get the ViewHolder back to get fast access to the TextView
        // and the ImageView.
        holder = (ViewHolder) convertView.getTag();
        
      }

      // Bind the data efficiently with the holder.
      
      holder.itemName.setText(listItems.get(position).getItemName());
      int q = listItems.get(position).getQuanlity();
      holder.itemQuanlity.setText((q<10?"0":"")+q);
      
      holder.itemName.setTag(position);
      holder.itemQuanlity.setTag(position);
      holder.btnDelete.setTag(position);
      holder.btnMinus.setTag(position);
      holder.btnPlus.setTag(position);
      holder.btnNote.setTag(position);

      return convertView;
    }
    
    

      public android.widget.Filter getFilter() {
        return null;
      }

      public long getItemId(int position) {
    	  //Toast.makeText(this.context, "getItemId - position = "+position, 3000).show();
    	  if(listItems.isEmpty()) return 0;
    	  return listItems.get(position).getItemId();
    	//return position;
      }

      public int getCount() {
    	  //Toast.makeText(this.context, "getCount - listItems.size() = "+listItems.size(), 3000).show();
        return listItems.size();
      }

      public Object getItem(int position) {
    	//Toast.makeText(this.context, "getItem - position = "+position, 3000).show();
    	
        return listItems.get(position);
      }

	public void onClick(View v) {
		
		try{
			// TODO Auto-generated method stub
			position = (Integer) v.getTag();
			if(v.getId()==R.id.btnDelete) {
				if(builder!=null) return;
				
					builder = new AlertDialog.Builder(this.context);
					builder.setMessage("Bạn muốn xóa "+listItems.get(position).getItemName()+" không?")
					       .setCancelable(false)
					       .setPositiveButton("Có", this)
					       .setNegativeButton("Không", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					                dialog.cancel();
					           }
					       });
					alert = builder.create();
					alert.show();
					builder = null;
					//Toast.makeText(this.context, "position = "+position+"\nbtnDelete - "+listItems.get(position).getItemName(), 3000).show();
				
			}
			else if(v.getId()==R.id.btnMinus) {
				int quanlity = listItems.get(position).getQuanlity();
				//Toast.makeText(this.context, "position = "+position+"\nbtnMinus - "+listItems.get(position).getItemName(), 3000).show();
				if(quanlity<2){
					if(builder != null) return;
					builder = new AlertDialog.Builder(this.context);
					builder.setMessage("Bạn muốn xóa "+listItems.get(position).getItemName()+" không?")
					       .setCancelable(false)
					       .setPositiveButton("Có", this)
					       .setNegativeButton("Không", new DialogInterface.OnClickListener() {
					           public void onClick(DialogInterface dialog, int id) {
					                dialog.cancel();
					           }
					       });
					alert = builder.create();
					alert.show();
					builder = null;
				}
				else {
					
					listItems.get(position).minus();
				}
				
				this.notifyDataSetChanged();
			}
			else if(v.getId()==R.id.btnPlus) {
				//Toast.makeText(this.context, "position = "+position+"\nbtnMinus - "+listItems.get(position).getItemName(), 3000).show();
				
				listItems.get(position).plus();
				
				this.notifyDataSetChanged();
			}
			else if(v.getId()==R.id.btnNote) {
				//Toast.makeText(this.context, "add note... = "+position, 3000).show();
				if(noticeIntent==null){
					noticeIntent = new Intent(this.context, ItemNoteScreen.class);
					noticeIntent.putExtra("title", "Ghi chú cho "+listItems.get(position).getQuanlity()+" "+listItems.get(position).getItemName());
					noticeIntent.putExtra("value", listItems.get(position).getNote());
					//this.context.startActivity(notice);
					((Activity) this.context).startActivityForResult(noticeIntent, IntentAction.EDIT_NOTE_ACTION);
					noticeIntent=null;
				}
			}
			else{
				if(!listItems.get(position).getNote().equals(""))
					Toast.makeText(this.context, listItems.get(position).getItemName()+
							"\n#Số lượng: "+listItems.get(position).getQuanlity()+
							"\n#Ghi chú: "+listItems.get(position).getNote(),
							2000).show();
				else
					Toast.makeText(this.context, listItems.get(position).getItemName()+
							"\n#Số lượng: "+listItems.get(position).getQuanlity()+
							"\n#Ghi chú: Không có",
							1200).show();
			}
		}catch(Exception e){
			//Toast.makeText(this.context, "exception = "+e.getMessage() +"\n"+e.getStackTrace()[0].toString()+"\n"+"\n"+e.getStackTrace()[1].toString()+"\n"+"\n"+e.getStackTrace()[2].toString()+"\n"+"\n"+e.getStackTrace()[3].toString()+"\n", 3000).show();
			e.printStackTrace();
		}
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		// TODO Auto-generated method stub
		//confirm delete - ok
		listItems.remove(position);
		//Toast.makeText(this.context, "delete - position "+position,3000).show();
		this.notifyDataSetChanged();
	}

	public void addOrderItem(OrderItem orderItem) {
		// TODO Auto-generated method stub
		this.listItems.add(orderItem);
	}
}

class ViewHolder {
    TextView itemName;
    ImageButton btnMinus;
    TextView itemQuanlity;
    ImageButton btnPlus;
    ImageButton btnNote;
    ImageButton btnDelete;
}